#!/usr/bin/python

class TikzPicture:
    def __init__(self, values, stddev, total, totaldev):
        self.values = values
        self.std_dev = stddev
        self.total = total
        self.totaldev = totaldev
        
        processing = total - sum(values)
        print "total = " + str(total)
        print "values = " + str(values)
        print "processing = " + str(processing)
        if processing > 0:
            self.values.append(processing)
            self.std_dev.append(0.0)
        self.normvalues = map(lambda x:x*1.0/total, values)


    def getColor(self, value, index, last=0):
        if last:
            return "gray!40"

        colors = ["steelblue", "bluegreen", "olivedrab", "darkorange", "red", "hotpink"]
        if index >= len(colors):
            return "gray!20"
        return colors[index]
        
        count = len(self.values)-1
        
        fill=1.0*(index+1)/count
        
        return "gray!%s" % (str(int(10 + 80*fill)))
        
        if value < 0.2:
            return "green!%s" % (str(int(30 + 50/0.2*value)))
        if value < 0.5:
            return "blue!%s" % (str(int(30 + 50/0.5*value)))
        if value > 0.5:
            return "red!%s" % (str(int(30 + 50/0.5*value)))

    def draw(self, width=15, height=0.7, caption="Some picture...", spaceing=0.2):
        pic = '\\begin{figure} \n \\begin{tikzpicture}[text centered]  \n \t\\footnotesize\n'
        
        ''' Calc the space for spacings '''
        sp = (len(self.values)-1)*spaceing      
        
        offset=0.0
        i = 0
        
        ''' Define colors '''
        pic +=  "\t\\xdefinecolor{steelblue}{RGB}{79,148,205} \n" \
                "\t\\xdefinecolor{bluegreen}{RGB}{102,205,170} \n" \
                "\t\\xdefinecolor{olivedrab}{RGB}{105,139,34} \n" \
                "\t\\xdefinecolor{darkorange}{RGB}{205,102,0} \n" \
                "\t\\xdefinecolor{red}{RGB}{142,35,35} \n" \
                "\t\\xdefinecolor{hotpink}{RGB}{139,58,98} \n\n" \
        
        ''' Draw the different measurments according to the length '''
        for val in self.normvalues:
            if i == len(self.values)-1:
                c=self.getColor(val, i, )
                #c=self.getColor(val, i, 1, )
            else:
                c=self.getColor(val, i)
        
            boxspecs = {"x1":offset, "y1":1.5, "x2":offset+val*(width-sp), "y2":1.5+height, \
                        "height2":1.5+height+0.2, "height3":1.5+height+5, "middle":offset+val*(width-sp)/2, \
                        "dliney1" :1.5+height+0.2, "dliney2":1.5+height+0.2+1.5, \
                        "w":width, "textboxwidth":2, "color":c, \
                        "time":self.values[i], "dev":self.std_dev[i], "total":self.total, "totaldev":self.totaldev}
            
            pic += "\t\\draw[line width=1pt] (%(x1).2f,%(y1).2f) rectangle (%(x2).2f,%(y2).2f);\n" % boxspecs
            pic += "\t\\fill[color=%(color)s] (%(x1).2f,%(y1).2f) rectangle (%(x2).2f,%(y2).2f);\n" % boxspecs
            
            if i != (len(self.values)-0): #and boxspecs["textboxwidth"] > 2:
                pic +=  "\t\draw [white,decorate,decoration={brace,amplitude=0pt}]" \
                        "(%(x1).2f,%(height2).2f) -- (%(x2).2f,%(height2).2f)" \
                        "node [black,midway,above=0pt,xshift=0, text width=%(textboxwidth).2fcm] {%(time).2f (%(dev).2f) ms};\n" % boxspecs
                pic +=  "\t\draw[dashed]  (%(middle).2f, %(dliney1).2f) -- (%(middle).2f, %(dliney2).2f) " \
                        "node [text width=4.00cm, above=8pt]{%(time).2f (%(dev).2f) ms};\n" % boxspecs
            
            pic += "\n"

            offset += val*(width-sp) + spaceing
            i += 1
            
        ''' End picture and put a caption '''
        pic +=  "\t\draw [gray,decorate,decoration={brace,amplitude=8pt}]" \
                "(%(w).2f,0.5) -- (0.0,0.5)" \
                "node [black,midway,below=9pt,xshift=0, text width=6cm] {Total Time:\\\\%(total).2f (%(totaldev).2f) ms};\n" % boxspecs
        pic += " \\end{tikzpicture}\n \\caption{%s}\n \\label{fig:...}\n\\end{figure}\n" % (caption)
        
        print pic


#pic = TikzPicture([3, 1, 5, 6], 17)
#pic.draw(["green!50", "blue!50", "red!50", "gray!40"])
